package com.yh.workbill.ui.count;

import com.yh.workbill.model.Count;
import com.yh.workbill.model.WorkBill;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class CountViewModel extends ViewModel {

    private MutableLiveData<String> mText;
    private MutableLiveData<List<Count>> countDatas;

    public CountViewModel() {
        mText = new MutableLiveData<>();
        mText.setValue("This is dashboard fragment");
    }

    public LiveData<String> getText() {
        return mText;
    }


    public MutableLiveData<List<Count>> getCountDatas() {
        if (countDatas == null) {
            countDatas = new MutableLiveData<>();
        }
        return countDatas;
    }

    public void addCountDatas(int index, Count... counts) {
        List<Count> value = countDatas.getValue();
        if (value == null) {
            value = new ArrayList<>();
        }
        List<Count> temp = Arrays.asList(counts);
        if (index == 0) {//新增
            value.addAll(0, temp);
            countDatas.postValue(value);
        } else {//更多
            value.addAll(temp);
            countDatas.postValue(value);
        }
    }

    public void update() {
        List<Count> value = countDatas.getValue();
        if (value == null) {
            value = new ArrayList<>();
        }
        countDatas.postValue(value);
    }

}